package com.assessment.servermanager.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.assessment.servermanager.commen.entity.PageQuery;
import com.assessment.servermanager.commen.page.TableDataInfo;
import com.assessment.servermanager.commen.utils.StringUtils;
import com.assessment.servermanager.entity.TbAdvice;
import com.assessment.servermanager.entity.TbApprove;
import com.assessment.servermanager.entity.bo.TbApproveBo;
import com.assessment.servermanager.entity.vo.TbApproveVo;
import com.assessment.servermanager.mapper.TbApproveMapper;
import com.assessment.servermanager.service.ITbAdviceService;
import com.assessment.servermanager.service.ITbApproveService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author jiajia
 * @date 2023-08-28
 */
@Service
public class TbApproveServiceImpl implements ITbApproveService {
    @Autowired
    private TbApproveMapper tbApproveMapper;

    @Autowired
    private ITbAdviceService adviceService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TbApprove selectTbApproveById(Long id) {
        return tbApproveMapper.selectById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tbApproveBo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public TableDataInfo<TbApproveVo> selectTbApproveList(TbApproveBo tbApproveBo, PageQuery pageQuery) {
        QueryWrapper<TbApprove> queryWrapper = new QueryWrapper<TbApprove>();
        queryWrapper.eq(tbApproveBo.getEid() != null, "ap.eid", tbApproveBo.getEid());
        queryWrapper.eq(StringUtils.isNotEmpty(tbApproveBo.getType()), "ap.type", tbApproveBo.getType());
        queryWrapper.apply(ObjectUtil.isNotNull(tbApproveBo.getStartTime()), "startTime between" + tbApproveBo.getStartTime() + "and" + tbApproveBo.getEndTime());
        queryWrapper.apply(ObjectUtil.isNotNull(tbApproveBo.getEndTime()), "endTime between" + tbApproveBo.getStartTime() + "and" + tbApproveBo.getEndTime());
        queryWrapper.eq(tbApproveBo.getState() != null, "ap.state", tbApproveBo.getState());
        queryWrapper.eq(tbApproveBo.getUid() != null, "ap.uid", tbApproveBo.getUid());
        queryWrapper.eq(tbApproveBo.getDid() != null, "ap.did", tbApproveBo.getDid());
        queryWrapper.eq("ap.delFlag",0);
        Page<TbApproveVo> page = tbApproveMapper.selectPageApproveList(pageQuery.build(), queryWrapper);
        return TableDataInfo.build(page);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tbApprove 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTbApprove(TbApprove tbApprove) {
        return tbApproveMapper.insert(tbApprove);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tbApprove 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateTbApprove(TbApprove tbApprove) {
        //将审批结果更新到申请表中
        int result = tbApproveMapper.updateById(tbApprove);
        //将审批结果添加到通知表
        TbAdvice advice = new TbAdvice();
        advice.setContent(tbApprove.getState() == 1 ? "您的审批已通过" : "您的审批被拒绝");
        advice.setTitle("您的审批有新的结果，请查看！");
        advice.setType(1);
        advice.setCreateDate(new Date());
        adviceService.insertTbAdvice(advice);
        return result;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTbApproveByIds(Long[] ids) {
        return tbApproveMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTbApproveById(Long id) {
        return tbApproveMapper.deleteById(id);
    }
}
